package com.newtouch.bxzs.database.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.newtouch.bxzs.common.base.model.UserPositionMap;
import com.newtouch.bxzs.database.model.bo.SimplePositionBO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 用户岗位mapper:
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/1/8 14:07</td>
 * <td>tianshangzhu</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author tianshangzhu
 * @version 1.0.0
 * @since 1.0.0
 */
@Mapper
public interface UserPositionMapMapper extends BaseMapper<UserPositionMap> {
    /**
     * 删除用户的旧的后台岗位
     *
     * @param userId 用户id
     */
    @Delete("DELETE FROM ASSISTANT_USER_POSITION_MAP WHERE USER_ID = #{userId} AND POSITION_ID IN " +
            "(SELECT POSITION_ID FROM ASSISTANT_POSITION WHERE TERMINAL_TYPE = 2)")
    void deleteOldPcPositionByUserId(@Param("userId") String userId);


    /**
     * 查询用户岗位名称和岗位id
     *
     * @param userId       用户id
     * @param terminalType 用户所属终端
     * @return SimplePositionBO
     */
    List<SimplePositionBO> selectUserPositionName(@Param("userId") String userId, @Param("terminalType") String terminalType);

    /**
     * 批量删除用户岗位中间表中的数据
     *
     * @param positionIdList 岗位id
     */
    void deleteByPositionIdBatch(@Param("positionIdList") List<String> positionIdList);

    /**
     * 删除用户岗位中间表中的数据
     *
     * @param userIdList 用户id
     */
    void deleteByUserIdBatch(@Param("userIdList") List<String> userIdList);

    /**
     * 批量插入数据
     *
     * @param list 数据
     */
    void saveBatch(@Param("list") List<UserPositionMap> list);

    /**
     * 判断岗位是否在用户岗位中间表存在
     *
     * @param positionId 岗位id
     * @return 是否存在，null不存在
     */
    @Select("SELECT 1 FROM ASSISTANT_USER_POSITION_MAP WHERE POSITION_ID = #{positionId} LIMIT 1")
    Integer checkExists(@Param("positionId") String positionId);

    /**
     * 查出用户的所有用户岗位中间表的id
     *
     * @param userId       用户id
     * @param terminalType 用户所属终端
     * @return 用户岗位中间表id
     */
    List<String> selectByUserId(@Param("userId") String userId, @Param("terminalType") String terminalType);
}
